package com.hgy.fxui.controller;

import com.hgy.fxui.dao.DrugDao;
import com.hgy.fxui.dao.PurchaseOrderDao;
import com.hgy.fxui.dao.SupplierDao;
import com.hgy.fxui.dao.UserDao;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@RestController
public class MainController {
    @FXML
    private StackPane contentArea;

    @FXML
    private VBox menuVBox;

    @FXML
    private ToggleGroup sideMenuGroup;

    private ToggleButton lastSelectedButton;    private final DrugDao drugDao;

    private final SupplierDao supplierDao;

    private final PurchaseOrderDao purchaseOrderDao;
    
    private final UserDao userDao;

    @FXML
    public void initialize() {
        // 确保ToggleGroup不为空
        if (sideMenuGroup == null) {
            sideMenuGroup = new ToggleGroup();
        }

        // 为所有的ToggleButton设置ToggleGroup
        menuVBox.getChildren().filtered(node -> node instanceof ToggleButton)
                .forEach(node -> {
                    ToggleButton button = (ToggleButton) node;
                    button.setToggleGroup(sideMenuGroup);
                });

        // 设置第一个按钮为默认选中
        if (!menuVBox.getChildren().isEmpty() && menuVBox.getChildren().get(0) instanceof ToggleButton) {
            ((ToggleButton) menuVBox.getChildren().get(0)).setSelected(true);
        }

        // 确保始终有一个按钮被选中
        sideMenuGroup.selectedToggleProperty().addListener((obs, oldToggle, newToggle) -> {
            if (newToggle == null && oldToggle != null) {
                oldToggle.setSelected(true);
            }
        });
    }

    @FXML
    private void showDrugManagement() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/DrugManagement.fxml"));
            Parent root = loader.load();
            DrugManagementController controller = loader.getController();

            // 1. 先设置依赖
            controller.setDrugDao(drugDao);

            // 2. 执行完整的初始化（包含UI设置+数据加载）
            controller.fullInitialize();

            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);
        } catch (Exception e) {
            log.error("加载药品管理界面失败", e);
        }
    }

    @FXML
    private void showSupplierManagement() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/SupplierManagement.fxml"));
            Parent root = loader.load();
            SupplierManagementController controller = loader.getController();

            // 1. 先设置依赖
            controller.setSupplierDao(supplierDao);

            // 2. 执行完整的初始化
            controller.fullInitialize();

            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);
        } catch (Exception e) {
            log.error("加载药品管理界面失败", e);
        }
    }

    @FXML
    private void showPurchaseManagement() throws IOException {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/PurchaseManagement.fxml"));
            Parent root = loader.load();
            PurchaseManagementController controller = loader.getController();

            // 1. 先设置依赖
            controller.setPurchaseOrderDao(purchaseOrderDao);
            controller.setSupplierDao(supplierDao);
            controller.setDrugDao(drugDao);

            // 2. 执行完整的初始化
            controller.fullInitialize();

            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);
        } catch (Exception e) {
            log.error("加载药品管理界面失败", e);
        }
    }    @FXML
    private void showUserManagement() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/UserManagement.fxml"));
            Parent root = loader.load();
            UserManagementController controller = loader.getController();
            controller.setUserDao(userDao);
            controller.fullInitialize();
            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);
        } catch (Exception e) {
            log.error("加载用户管理界面失败", e);
        }
    }

    @FXML
    private void showInventoryManagement() {
        // TODO: 实现库存管理界面加载
    }
    @FXML
    public void showDashboard() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/dashboard.fxml"));
            Parent root = loader.load();
            DashboardController controller = loader.getController();

            // 设置依赖
            controller.setDrugDao(drugDao);
            controller.setSupplierDao(supplierDao);
            controller.setPurchaseOrderDao(purchaseOrderDao);
            
            // 初始化
            controller.fullInitialize();

            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);
        } catch (Exception e) {
            log.error("加载数据大屏失败", e);
        }
    }
}